package my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.annotation.propagation.never.eachcall.service;

import javax.annotation.Resource;

import org.springframework.aop.framework.AopContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component
public class TxNeverEachService {
    /**
     * jdbc模版，用于操作数据库
     */
    @Resource
    private JdbcTemplate jdbcTemplate;

    /** 在开启了事务的方法m1中调用m2 */
	@Transactional(propagation = Propagation.REQUIRED)
    public void m1() {
		//m1 do something1
        String sql = "insert into tx_study (name,age) values (?,?)";
        jdbcTemplate.update(sql, "TxRequired_m1_something1", 30);
        TxNeverEachService o = (TxNeverEachService)AopContext.currentProxy();
        o.m2();
        //m1 do something2
        jdbcTemplate.update(sql, "TxRequired_m1_something2", 30);
	}
	
	@Transactional(propagation = Propagation.NEVER)
	public void m2() {
        String sql = "insert into tx_study (name,age) values (?,?)";
        jdbcTemplate.update(sql, "TxNever_m2", 30);       
	}
}
